﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using IndieStudio.BugsBang.Utility;
using UnityEngine.Events;

///Developed By Indie Studio
///https://assetstore.unity.com/publishers/9268
///www.indiestd.com
///info@indiestd.com

namespace IndieStudio.BugsBang.Game
{
	[DisallowMultipleComponent]
	public class Booster : MonoBehaviour
	{
		/// <summary>
		/// The number text of the booster.
		/// </summary>
		public Text number;

		/// <summary>
		/// The type of the booster.
		/// </summary>
		public Type type;

		/// <summary>
		/// The remaining value/count of the booster.
		/// </summary>
		private int value;

		/// <summary>
		/// References array.
		/// You can attach references/objects to be accessed especially in the UIEvents or others scripts
		/// </summary>
		public Transform[] references;

		// Use this for initialization
		void Awake ()
		{
			//Load booster value
			value = LoadValue ();
			if (value == -1) {
				//If there is no data , use default value
				ResetValue ();
			}

			//Refresh/Set the number text value
			RefreshNumberText ();
		}

		/// <summary>
		/// Get the value/count of the booster.
		/// </summary>
		/// <returns>The value/count of the booster.</returns>
		public int GetValue ()
		{
			return value;
		}

		/// <summary>
		/// Decrease booster's value by one.
		/// </summary>
		public void DecreaseValue ()
		{
			if (value == 0) {
				return;
			}
			
			value--;
			RefreshNumberText ();
			SaveValue ();
		}

		/// <summary>
		/// Load booster's value/count.
		/// </summary>
		/// <returns>The value/count of the booster.</returns>
		public int LoadValue ()
		{
			if (type == Type.TIME_PAUSE) {
				return DataManager.GetTimePauseCount ();
			} else if (type == Type.LIVES_INCREMENT) {
				return DataManager.GetLivesIncrementCount ();
			} else if (type == Type.BANG_TARGETS) {
				return DataManager.GetTargetsBangCount ();
			}

			return 0;
		}

		/// <summary>
		/// Save the value/count of the booster.
		/// </summary>
		public void SaveValue ()
		{
			if (type == Type.TIME_PAUSE) {
				DataManager.SaveTimePauseCount (value);
			} else if (type == Type.LIVES_INCREMENT) {
				DataManager.SaveLivesIncrementCount (value);
			} else if (type == Type.BANG_TARGETS) {
				DataManager.SaveTargetsBangCount (value);
			}
		}

		/// <summary>
		/// Reset the value/count of the booster.
		/// </summary>
		public void ResetValue ()
		{
			value = 3;

			if (type == Type.TIME_PAUSE) {
				DataManager.SaveTimePauseCount (value);
			} else if (type == Type.LIVES_INCREMENT) {
				DataManager.SaveLivesIncrementCount (value);
			} else if (type == Type.BANG_TARGETS) {
				DataManager.SaveTargetsBangCount (value);
			}
			RefreshNumberText ();
		}

		/// <summary>
		/// Refresh number text value.
		/// </summary>
		private void RefreshNumberText ()
		{
			if (number != null)
				number.text = value.ToString ();
		}

		public enum Type
		{
			TIME_PAUSE,
			LIVES_INCREMENT,
			BANG_TARGETS
		};

	}

}